package com.moon.game.command.guest.bag;

import java.util.ArrayList;
import java.util.List;


import com.moon.game.clazz.PlayerItemsProto.PlayerItems.PlayerItem;
import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.exception.GException;
import com.moon.game.resource.model.Item;
import com.moon.game.service.ItemService;
import com.moon.game.service.PlayerItemService;
import com.moon.game.util.Utils;
import com.moon.game.vos.PlayerItemVo;
import com.moon.game.vos.PlayerVo;

/**
 * @description 查看行囊
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-23 上午11:18:47
 */
public class ViewBagCmd extends BaseCmd{
	
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null || ps.length < 2) {
			doResponsePsError(req);
			return;
		}
		int type = Integer.parseInt(ps[0]);
		int page = Integer.parseInt(ps[1]);
		String playerId = req.getPlayerId();
		PlayerVo player = getPlayerVo(req);
		if (type == Constants.PLAYER_ITEM_TYPE) {
			List<PlayerItem> playerItems = PlayerItemService.getPlayerItems(playerId);
			int usedBurden = 0;
			if (playerItems != null) {
				for (PlayerItem playerItem : playerItems) {
					if (playerItem.getIsUse()) {
						continue;
					}
					usedBurden += playerItem.getWeight();
				}
			}
			ArrayList<PlayerItemVo> pageItems = new ArrayList<PlayerItemVo>();
			List<Object> objs = Utils.page(playerItems, page, Constants.PAGE_SIZE);
			for (Object obj : objs) {
				PlayerItem playerItem = (PlayerItem)obj;
				if (playerItem.getIsUse()) {
					continue;
				}
				PlayerItemVo vo = new PlayerItemVo();
				Utils.copyProperties(vo, playerItem);
				pageItems.add(vo);
			}
			if (playerItems != null && playerItems.size() > 0) {
				req.setVO("total_page", Utils.totalPage(playerItems.size(), Constants.PAGE_SIZE));
			}
			req.setVO("used_burden", usedBurden);
			req.setVO("burden", player.getBurden());
			req.setVO("player_items", pageItems);
		}else if (type == Constants.ITEM_OTHER_TYPE) {
			ArrayList<Item> items = ItemService.getAllItems(req.getPlayerId());
			int usedRoom = 0;
			if (items != null) {
				for (Item item : items) {
					usedRoom += item.getNum();
				}
			}
			ArrayList<Item> pageItems = new ArrayList<Item>();
			List<Object> objs = Utils.page(items, page, Constants.PAGE_SIZE);
			for (Object obj : objs) {
				Item item = (Item) obj;
				pageItems.add(item);
			}
			if (items != null && items.size() > 0) {
				req.setVO("total_page", Utils.totalPage(items.size(), Constants.PAGE_SIZE));
			}
			req.setVO("used_room", usedRoom);
			req.setVO("max_room", player.getMaxRoom());
			req.setVO("items", pageItems);
//			req.setVO("total_page", Utils.totalPage(items.size(), Constants.PAGE_SIZE));
		}
		req.setVO("curr_type", type);
		req.setVO("curr_page", page);
		System.err.println(page);
		response(req, req.getClientResponse());
	}

}
